﻿package code {
	
	import flash.display.Shape;
	
	// class that represents a base shape.  based off of polygon code from schwartz
	public class BaseShape extends Shape {

		public var radius:Number;     // radius from center of polygon
		protected var sides:Number;      // number of sides
		public var color:uint;        // color of polygon
		protected var thickness:Number;  // thickness of outer line
		protected var hollow:Boolean;    // is the polygon hollow?
		public var vertices:Array;
		public var linesArray:Array;
		public var angle:Number;
		public var oldAngle:Number;
		
		public function BaseShape(radius:Number, sides:Number, color:uint, thickness:Number, hollow:Boolean, angle:Number) {
			this.radius = radius;
			this.sides = sides;
			this.color = color;
			this.thickness = thickness;
			this.hollow = hollow;
			this.angle = angle;
			vertices = new Array();
			linesArray = new Array();
			oldAngle = 0;
		}
		
		// acts as a abstract method to be overriden
		public function makeVertices():void{
			
		}
		
		// rotates the vertices using a rotation matrix
		public function rotateVertices():void{
			var rotationAngle = angle - oldAngle;
			for(var j:int = 0; j < vertices.length; j++){  // goes through each vertex and rotates it
				var vert1:Vertex = vertices[j];  // pulls the vertex to test

				var cos:Number = Math.cos(rotationAngle);  //gets cos and sin of angle to rotate to
				var sin:Number = Math.sin(rotationAngle);
				
				var x2 = vert1.x * cos + vert1.y * -sin;  // multiple x and y matrix by the rotation matrix
				var y2 = vert1.x * sin + vert1.y * cos;
				
				vertices[j] = new Vertex(x2, y2);  // assigns the new vertex
			}
			oldAngle = angle;
		}
		
		// render the polygon: from schwartz
		public function renderVertices():void {
			
			// set fill and hollowness:
			graphics.beginFill(color,alpha=(hollow?0:1));
			
			// set border:
			graphics.lineStyle(thickness,color,alpha=1);
			
			// start at this vertex (x=radius,y=0 from center)
			graphics.moveTo(Vertex(vertices[0]).x, Vertex(vertices[0]).y);
			// draw line to each vertex going clockwise from starting vertex:
			for(var side:uint=1; side < sides; side++) {
				graphics.lineTo(Vertex(vertices[side]).x, Vertex(vertices[side]).y);
			}
			
			// connect the last vertex to starting vertex:
			graphics.lineTo(Vertex(vertices[0]).x, Vertex(vertices[0]).y);
			
			// done:
			graphics.endFill();
		}
	}
	
}
